﻿/// <reference path="jquery-1.6.2-vsdoc.js" />

(function ($) {

    $.fn.removeValidationInline = function () {
        var $form = $(this);
        if ($form.is("form")) {
            $form.find(".formError").animate({ "opacity": 0 }, function () {
                $(this).remove();
            });
        }
    };

    //Change <span> validation message display to top right corner style prompt
    $.fn.makeValidationInline = function (options) {

        var settings = {
            showTriangle: true,//是否显示arrow
            promptPosition: "topRight"
        };
        $.extend(settings, options);

        return this.each(function () {

            var $form = $(this);

            //#region message prompt functions
            /*
            * Inline Form Validation Engine 1.3.9.5, jQuery plugin
            * 
            * Copyright(c) 2009, Cedric Dugas
            * http://www.position-relative.net
            *	
            * Form validation engine which allow custom regex rules to be added.
            * Licenced under the MIT Licence
            * Modified by Jeffrey Lee, http://blog.darkthread.net, to support ASP.NET MVC 3
            */
            function buildPrompt(caller, promptText) {			// ERROR PROMPT CREATION AND DISPLAY WHEN AN ERROR OCCUR
                var $divFormError = $("<div  class='formError'  />");
                var $formErrorContent = $("<div class='formErrorContent' />");
                $formErrorContent.html(promptText);
                var $caller = $(caller);
                if ($caller.parent(".inputWrapper20120722").length == 0) {
                    $caller.wrap($("<span class='inputWrapper20120722' />"));
                }
                $divFormError.addClass($caller.attr("id")).append($formErrorContent).insertAfter($caller.parent(".inputWrapper20120722"));
                if (settings.showTriangle) {		// NO TRIANGLE ON MAX CHECKBOX AND RADIO
                    var $arrow = $("<div class='formErrorArrow' />");
                    $divFormError.append($arrow)
                    if (settings.promptPosition == "bottomLeft" || settings.promptPosition == "bottomRight") {
                        $arrow.addClass("formErrorArrowBottom")
                        $arrow.html('<div class="line1"><!-- --></div><div class="line2"><!-- --></div><div class="line3"><!-- --></div><div class="line4"><!-- --></div><div class="line5"><!-- --></div><div class="line6"><!-- --></div><div class="line7"><!-- --></div><div class="line8"><!-- --></div><div class="line9"><!-- --></div><div class="line10"><!-- --></div>');
                    }
                    if (settings.promptPosition == "topLeft" || settings.promptPosition == "topRight") {
                        $divFormError.append($arrow)
                        $arrow.html('<div class="line10"><!-- --></div><div class="line9"><!-- --></div><div class="line8"><!-- --></div><div class="line7"><!-- --></div><div class="line6"><!-- --></div><div class="line5"><!-- --></div><div class="line4"><!-- --></div><div class="line3"><!-- --></div><div class="line2"><!-- --></div><div class="line1"><!-- --></div>');
                    }
                }
                setPosition($caller, $divFormError);

                $(".formErrorContent").click(function () {
                    $(this).parent(".formError").animate({ "opacity": 0 }, function () {
                        $(this).remove();
                    });

                });
                return $divFormError.animate({ "opacity": 0.8 }, function () { return true; });
            }



            function updatePromptText(caller, promptText) {	// UPDATE TEXT ERROR IF AN ERROR IS ALREADY DISPLAYED
                var $caller = $(caller);
                var $updateThisPrompt = $("div." + caller.id);
                if ($updateThisPrompt.length == 0) {
                    return buildPrompt(caller, promptText);
                }
                $updateThisPrompt.find(".formErrorContent").html(promptText);
                setPosition($caller, $updateThisPrompt);
                return $updateThisPrompt;
            }


            function setPosition($elementContainer, $errorContainer) {
                var $arrow = $elementContainer.find(".formErrorArrow");
                var positionX = $elementContainer.width();
                var positionY = $errorContainer.outerHeight() + $elementContainer.height();
                //set the position of display
                if (settings.promptPosition == "topRight") {
                    positionY = -(positionY);
                    positionX -= 40;
                }
                if (settings.promptPosition == "topLeft") {
                    positionY = -(positionY);
                    positionX = 0;
                }
                if (settings.promptPosition == "centerRight") {
                    positionY = -((positionY) >> 1);
                    positionX += 10;
                }

                if (settings.promptPosition == "bottomLeft") {
                    positionY = $arrow.height() >> 1;
                    positionX = 0;
                }

                if (settings.promptPosition == "bottomRight") {
                    positionY = $arrow.height() >> 1;
                    positionX -= 30;
                }
                $errorContainer.css({
                    marginTop: positionY,
                    marginLeft: positionX
                });
                return $errorContainer;
            }

            function showInvalidPrompt(caller, message) {
                updatePromptText(caller, message);
                //$(caller).focus();
            }

            function hideInvalidPrompt(caller) {
                $("div." + caller.id).remove();
            }
            //#endregion

            function custErrorPlacement(error, inputElement) {  // 'this' is the form element
                var container = $(this).find("[data-valmsg-for='" + inputElement[0].name + "']");
                if (container.length == 0) {
                    container = $("<span data-valmsg-for='" + inputElement[0].name + "' />");
                    $(inputElement).after(container);
                }
                var msg = error.text();
                if (msg.length > 1) {
                    error.data("inv_msg_prompt", showInvalidPrompt(inputElement[0], msg));
                }
                else {
                    hideInvalidPrompt(inputElement[0]);
                }
                
            }

            function custSuccess(error) {  // 'this' is the form element
                var p = error.data("inv_msg_prompt");
                if (p) {
                    p.remove();
                }
            }



            //#region change errorPlacement
            var valdSettings = $form.data("validator").settings;
            valdSettings.errorElement = "li";
            valdSettings.errorPlacement = $.proxy(custErrorPlacement, this);
            valdSettings.success = $.proxy(custSuccess, this);
            $form.validate(valdSettings);
            //#endregion
        });
    }
})(jQuery);